#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>

int multi_end(FILE *input_file, unsigned int simb)
{
	unsigned int simb1;
	int end;

	if(simb == '*'){
		simb1 = fgetc(input_file);

		if (simb1 == '/')
			return 1;

		if(simb1 == EOF || simb == EOF)
			return 0;

			
			end = multi_end(input_file, simb1);
			return end;
	
	}
return 4;
}

void in_file(unsigned int *buffer, FILE *output_file)
{
	int i=0;

	while(1){
		if (buffer[i] == '\0')
			return;

		fputc(buffer[i], output_file)	;
		i++;
	}
}

void oneline(FILE *input_file)
{
	unsigned int simb;

	while(1) {
		simb = fgetc(input_file);
		if(simb == '\n' || simb == '\0' || simb == EOF)
			break;
	}
return;
}

int multiline(FILE *input_file)
{
	unsigned int simb;
	int end;
	
	while(1){
		simb = fgetc(input_file);

		if (simb == EOF){
			printf("error\n");
			return 0;
		}

		end = multi_end(input_file, simb);
		if (end == 0 || end == 1)
			return end;
		
	}



}

int assignment( unsigned int **buffer, unsigned int simbol)
{
	unsigned int *numbers;
	static int counter = 0;
	counter++;
	numbers = (unsigned int*) realloc(*buffer, counter * sizeof(unsigned int));

	 if (numbers != NULL)
     {
		numbers[counter - 1] = simbol;   
        *buffer = numbers;
	 }
     else
     {
       free (buffer);                                   
	   printf("error");
	   return 0;       
     }
return 1;
}

int check(unsigned int **buffer, unsigned int simbol1, FILE *input_file)
{
	int gramma = 1;
	unsigned int simbol;
	fpos_t pos;

	if (simbol1 == '/'){
		simbol = fgetc(input_file);
		if(simbol == '/'){
			oneline(input_file); 
		}
		else
		{
			if(simbol == '*')
			{
				gramma = multiline(input_file);
			}
			else{
				assignment(buffer, simbol1);
				assignment(buffer, simbol);
				
			}
		}
	}
	else{
		if (simbol1 == '*')
		{
			fgetpos(input_file, &pos);
			
			if (fgetc(input_file) == '/')
				return 0;
			else
				fsetpos(input_file, &pos);
		}
		assignment(buffer, simbol1);
		
	}
	
	return gramma;
	
}

void main (int count, const char *values[])
{		
	FILE *input_file;
	FILE *output_file;
		
	unsigned int *buffer = NULL;

	int gramma;
	unsigned int simbol;
	
	if (count != 4){
		printf("fail arguments,,, ERROR\n");
		return;
	}
	
	input_file = fopen(values[2], "r");

	
	if (input_file == NULL ){
		printf(" Fail %s \n", "input_file");
		return;
	}

	while(1)
	{
		simbol = fgetc(input_file);

		if(simbol == EOF) break;
		gramma = check(&buffer, simbol, input_file);

		if(gramma == 0){
			printf("error in file\n");
			break;
		}
	}
	
	if(gramma)
	{
		output_file = fopen(values[3], "w");

		if (output_file == NULL ){
			printf(" Fail %s \n", "output_file");
			return;
		}
		assignment(&buffer, '\0');
		in_file(buffer, output_file);
		fclose(output_file);
	}

	printf("\n");
	system("pause");

	fclose(input_file);
	

	free(buffer);
}

